﻿using System;
using System.Collections.Generic;
using System.IO;

namespace SQLMap.Core.Provider
{

    /// <summary>
    /// <![CDATA[SQLMAP配置提供程序]]>
    /// </summary>
    public sealed class SqlMapConfigProvider : ISqlMapConfigProvider
    {

        /// <summary>
        /// <![CDATA[静态字典]]>
        /// </summary>
        private static Dictionary<string, Command> dictSqlMapData = new Dictionary<string, Command>();

        /// <summary>
        ///<![CDATA[配置SqlMap]]>
        /// </summary>
        public void Configure(Stream stream)
        {
            //序列化对象
            System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(SqlMap));
            object obj = xmlSerializer.Deserialize(stream);
            RegisterSqlMap(obj);
            stream.Close();
        }

        /// <summary>
        /// <![CDATA[配置SqlMap]]>
        /// </summary>
        /// <param name="textReader"></param>
        public void Configure(TextReader textReader)
        {
            System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(SqlMap));
            object obj = xmlSerializer.Deserialize(textReader);
            RegisterSqlMap(obj);
            textReader.Close();
        }

        /// <summary>
        /// <![CDATA[配置SqlMap]]>
        /// </summary>
        /// <param name="innerXml"></param>
        public void Configure(string innerXml)
        {
            TextReader textReader = new StringReader(innerXml);
            System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(SqlMap));
            object obj = xmlSerializer.Deserialize(textReader);
            RegisterSqlMap(obj);
            textReader.Close();
        }

        /// <summary>
        /// <![CDATA[配置SqlMap]]>
        /// </summary>
        /// <param name="assembly"><![CDATA[程序集]]></param>
        /// <param name="name"><![CDATA[name]]></param>
        public void Configure(System.Reflection.Assembly assembly, string name)
        {
            using (Stream stream = assembly.GetManifestResourceStream(name))
            {
                System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(SqlMap));
                object obj = xmlSerializer.Deserialize(stream);
                RegisterSqlMap(obj);//注册SQL-MAP
                stream.Close();
            }
        }

        /// <summary>
        /// <![CDATA[从配置文件中载入]]>
        /// </summary>
        /// <param name="filename"><![CDATA[配置文件绝对路径]]></param>
        public void LoadConfiguration(String filename)
        {
            Configure(File.OpenRead(path: filename));
        }

        /// <summary>
        /// <![CDATA[根据name获取CommandInfo对象]]>
        /// </summary>
        /// <param name="sqlId"><![CDATA[SQL-MAP Name]]></param>
        /// <returns></returns>
        public Command GetCommandFromSqlMapById(string sqlId)
        {
            if (dictSqlMapData.ContainsKey(sqlId) == false) { return null; }
            return dictSqlMapData[sqlId];
        }

        /// <summary>
        /// <![CDATA[注册一个CommandInfor对象到SqlMap]]>
        /// </summary>
        /// <param name="commandInfo"></param>
        public void RegisterSqlMap(Command commandInfo)
        {
            dictSqlMapData.Add(commandInfo.Id, commandInfo);
        }


        /// <summary>
        /// <![CDATA[监视文件]]>
        /// </summary>
        public void Watch()
        {
            System.IO.FileSystemWatcher fsw = new FileSystemWatcher("");
            fsw.Changed += new FileSystemEventHandler((o, f) =>
            {

            });
        }

        /// <summary>
        ///<![CDATA[将SQLMAP注册到字典中]]>
        /// </summary>
        /// <param name="obj"><![CDATA[SQLMAP对象]]></param>
        private void RegisterSqlMap(object obj)
        {
            SqlMap sqlMap = (SqlMap)obj;
            foreach (var item in sqlMap.CommandList)
            {
                dictSqlMapData.Add(item.Id, item);
            }
        }
    }
}
